package com.lmm.yydshtester.controller;

/**
 * @title: CommandController
 * @Description: 命令控制器
 * @Author Lmm
 * @Date: 2022/11/11 12:50
 * @Version 1.0
 */

import com.lmm.yydshtester.command.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 命令控制台类
 * */
@Slf4j
@Data
@Service("CommandController")
public class CommandController {

    @Autowired
    TestCaseUploadCommand testCaseUploadCommand;


    @Autowired
    TestCaseViewCommand testCaseViewCommand;


    @Autowired
    WorkerViewCommand workerViewCommand;


    @Autowired
    CaseRunCommand caseRunCommand;


    @Autowired
    CaseResultViewCommand caseResultViewCommand;

    @Autowired
    CaseResultClearCommand caseResultClearCommand;

    @Autowired
    CommandMenu commandMenu;

    private Map<String, BaseCommand> commandMap;

    private String menuString;
    /**
     * 初始化命令
     * */
    public void initCommandMap()
    {
        commandMap = new HashMap<>();
        commandMap.put(commandMenu.getKey(), commandMenu);
        commandMap.put(testCaseUploadCommand.getKey(), testCaseUploadCommand);
        commandMap.put(testCaseViewCommand.getKey(), testCaseViewCommand);
        commandMap.put(workerViewCommand.getKey(), workerViewCommand);
        commandMap.put(caseRunCommand.getKey(), caseRunCommand);
        commandMap.put(caseResultViewCommand.getKey(), caseResultViewCommand);
        commandMap.put(caseResultClearCommand.getKey(), caseResultClearCommand);

        commandMenu.setAllCommand(commandMap);
    }
    /**
     * 执行命令
     * */
    public void commandThreadRunning()
    {
        Thread.currentThread().setName("命令线程");

        while (true)
        {
            Scanner scanner = new Scanner(System.in);
            commandMenu.exec(scanner);
            String key = commandMenu.getCommandInput();
            BaseCommand command = commandMap.get(key);

            if (null == command)
            {
                System.err.println("无法识别[" + command + "]指令，请重新输入!");
                continue;
            }
            command.exec(scanner);
        }
    }
}
